// ---------------------------------------------------------------------------

#pragma hdrstop

#include "ModuleConf.h"

// ---------------------------------------------------------------------------

#pragma package(smart_init)

TModuleConf __fastcall TModuleConf::GetModuleConfig
(Classes::TComponent* AOwner, String conf) {
	TModuleConf ModuleConf;
	if (conf == "")
		return ModuleConf;

	TXMLDocument *doc = new TXMLDocument(AOwner);
	try {
		doc->LoadFromXML(conf);
		_di_IXMLDOMNodeRef refNode = doc->DocumentElement->DOMNode;
		_di_IXMLDOMNode root = refNode->GetXMLDOMNode();
		_di_IXMLDOMNodeList nodes;
		_di_IXMLDOMNode node;
		_di_IXMLDOMNode _node;
		_di_IXMLDOMNode attr;
		root->selectNodes("//Module/Grid", nodes);
		for (int i = 0; i < nodes->length; i++) {
			node = nodes->item[i];
			TGridInfo grid;

			grid.Name = "";
			if (!node->attributes->getNamedItem("Name", attr)) {
				grid.Name = attr->text;
			}

			grid.Table = "";
			if (!node->attributes->getNamedItem("Table", attr)) {
				grid.Table = attr->text;
			}

			grid.PrimaryKey = "";
			if (!node->attributes->getNamedItem("PrimaryKey", attr)) {
				grid.PrimaryKey = attr->text;
			}

			grid.ForeignKey = "";
			if (!node->attributes->getNamedItem("ForeignKey", attr)) {
				grid.ForeignKey = attr->text;
			}

			grid.AutoSelect = false;
			if (!node->attributes->getNamedItem("AutoSelect", attr)) {
				if (Trim(attr->text) == "true")
					grid.AutoSelect = true;
			}

			grid.DisplayDate = false;
			if (!node->attributes->getNamedItem("DisplayDate", attr)) {
				if (Trim(attr->text) == "true")
					grid.DisplayDate = true;
			}

			grid.DisplaySearchValue = false;
			if (!node->attributes->getNamedItem("DisplaySearchValue", attr)) {
				if (Trim(attr->text) == "true")
					grid.DisplaySearchValue = true;
			}

			grid.Service = "";
			if (!node->attributes->getNamedItem("Service", attr)) {
				grid.Service = attr->text;
			}

			grid.DateType = 1;
			if (!node->attributes->getNamedItem("DateType", attr)) {
				try {
					grid.DateType = StrToInt(attr->text);
				}
				catch(...) {
					grid.DateType = 1;
				}
			}

			grid.TreeName = "";
			if (!node->attributes->getNamedItem("TreeName", attr)) {
				grid.TreeName = attr->text;
			}

			grid.Parameter = "";
			if (!node->attributes->getNamedItem("Parameter", attr)) {
				grid.Parameter = attr->text;
			}

			ModuleConf.GridMap.insert(pair<int, TGridInfo>(i, grid));
		}
	}
	__finally {
		delete doc;
	}
	return ModuleConf;
}

// ---------------------------------------------------------------------------
int __fastcall TModuleConf::GridCount() {
	return GridMap.size();
}

// ---------------------------------------------------------------------------
TGridInfo* TModuleConf::GetGridByIndex(int index) {
	if (GridMap.find(index) != GridMap.end())
		return&(GridMap.find(index)->second);
	return NULL;
}

// ---------------------------------------------------------------------------
TGridInfo* TModuleConf::GetGridByTable(String table) {
	map<int, TGridInfo>::iterator it;
	for (it = GridMap.begin(); it != GridMap.end(); it++) {
		if (it->second.Table == table)
			return&(it->second);
	}
	return NULL;
}
// ---------------------------------------------------------------------------

